package dse.messages.multiversion;

import dse.Experiment;
import dse.ExperimentRecorder;
import dse.Version;
import dse.messages.Message;
import dse.messages.MessageHandler;
import dse.messages.MessageSender;
import dse.messages.MessageType;

public class VersionNumberResponseMessageHandler extends MessageSender
		implements MessageHandler {
	private Version version;
	private SVRHandler handler;
	private int round;
	
	public VersionNumberResponseMessageHandler(Experiment experiment, Version version, SVRHandler handler, int round) {
		super(experiment);
		// TODO Auto-generated constructor stub
		this.version = version;
		this.handler = handler;
		this.round = round;
	}

	@Override
	public boolean acceptMessage(Message message) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public void messageInterupted(ExperimentRecorder recorder, Message message,
			double time) {
		// TODO Auto-generated method stub
		if(ExperimentRecorder.debug)
			recorder.debug(this, "msgIntrpt: " + message, time);
		handler.readVersion(recorder, message.to(), null, time, round);
	}

	@Override
	public void messageReceived(ExperimentRecorder recorder, Message message,
			double time) {
		// TODO Auto-generated method stub
		if(ExperimentRecorder.debug)
			recorder.debug(this, "msgRcvd: " + message, time);
		if (message.messageType() != MessageType.VersionNumberResponse) {
			recorder.warn(
					"VersionNumberResponseMessageHandler recieved " + 
					message.messageType() + " message."
					);
			return;
		}			
		
		handler.readVersion(recorder, message.to(), version, time, round);
	}
}
